<template>
  <d2-container :filename="filename">
    <template slot="header">活动列表</template>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="listData"
      :pagination="pageData"
      :add-template="popTemplate"
      :edit-template="popTemplate"
      :form-options="formOptions"
      :rowHandle="rowHandle"
      @edit-item="onEdit"
      @del-item="onDel"
      @dialog-open="handleDialogOpen"
      @row-edit="handleRowEdit"
      @row-add="handleRowAdd"
      @dialog-cancel="handleDialogCancel">
      <el-button slot="header" style="margin-bottom: 5px" @click="onAdd">新增</el-button>
    </d2-crud>
  </d2-container>
</template>

<script>
import utils from '@/libs/util.gaed'
// import settings from '@/setting'
import ImageColumn from '@/components/custom-column/image'
import Upload from '@/components/custom-column/upload'
const BASE_URL = 'activity/organizations'

// address 活动地址
// address_lng 精度
// address_lat 纬度
export default {
  data () {
    return {
      listData: [],
      pageData: {},
      filename: __filename,
      columns: [
        {
          title: '标题',
          key: 'title'
        },
        {
          title: '描述',
          key: 'desc'
        },
        {
          title: '详情',
          key: 'details'
        },
        {
          title: '活动时间段',
          key: 'durationStr'
        },
        {
          title: '活动分类',
          key: 'typeStr'
        },
        {
          title: '状态',
          key: 'statusStr'
        },
        {
          title: '活动所在地区',
          key: 'areaStr'
        },
        {
          title: '缩略图',
          key: 'thumb',
          component: {
            name: ImageColumn,
            props: {
              dataKey: 'thumb'
            }
          }
        },
        {
          title: '主图',
          key: 'main_img',
          component: {
            name: ImageColumn,
            props: {
              dataKey: 'main_img'
            }
          }
        },
        {
          title: '机构代表',
          key: 'user_name'
        },
        {
          title: '状态',
          key: 'statusStr'
        }
      ],
      rowHandle: {
        width: '220px',
        custom: [
          {
            text: '删除',
            type: 'warning',
            size: 'small',
            emit: 'del-item'
          },
          {
            text: '编辑',
            size: 'small',
            emit: 'edit-item'
          }
        ]
      },
      data: [],
      popTemplate: {
        name: {
          title: '名称',
          value: ''
        },
        desc: {
          title: '描述',
          value: ''
        },
        content: {
          title: '内容',
          value: ''
        },
        thumb: {
          title: '缩略图',
          value: '',
          component: {
            name: Upload,
            props: {
              type: 'image'
            }
          }
        },
        main_img: {
          title: '主图',
          value: '',
          component: {
            name: Upload,
            props: {
              type: 'image'
            }
          }
        },
        user_name: {
          title: '机构代表',
          value: ''
        }
      },
      formOptions: {
        labelWidth: '80px',
        labelPosition: 'left',
        saveLoading: false
      }
    }
  },
  mounted () {
    this.initList()
  },
  methods: {
    initList () {
      utils.fetchData.call(this, BASE_URL).then(res => {
        this.listData = res.lists.map(d => {
          // d.updateTimeStr = d.update_time ? utils.formatDate(new Date(1000 * (+d.update_time)), settings.datetime_format) : ''
          d.statusStr = d.status < 0 ? '已拉黑' : '正常'
          d.userInfo = d.user_name + '（用户ID：' + d.user_id + '）'
          return d
        })
      }).catch(err => {
        console.warn(err)
      })
    },
    onAdd () {
      this.$refs.d2Crud.showDialog({ mode: 'add' })
    },
    onEdit ({ index }) {
      this.$refs.d2Crud.showDialog({
        mode: 'edit',
        rowIndex: index
      })
    },
    onDel ({ index, row }) {
      this.$confirm('即将删除当前评论, 是否继续?', row.title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = BASE_URL + '/' + row.id
        utils.deleteData.call(this, url).then(response => {
          console.log(response)
          this.initList()
        }).catch(err => {
          console.warn(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleRowAdd (dataObj, done) {
      console.log('当前添加的数据项为：', dataObj)
      utils.addData.call(this, BASE_URL, dataObj).then(res => {
        this.initList()
        this.$refs.d2Crud.closeDialog()
        done()
      }).catch(err => {
        console.warn(err)
      })
    },
    // 对行进行编辑
    handleRowEdit (dataObj, done) {
      let id = dataObj.row.id
      let url = BASE_URL + '/' + id
      utils.editData.call(this, url, dataObj.row).then(res => {
        this.initList()
        this.$refs.d2Crud.closeDialog()
        done()
      }).catch(err => {
        console.warn(err)
      })
    },
    // 弹窗点击取消关闭
    handleDialogCancel (done) {
      this.$refs.d2Crud.closeDialog()
      done()
    },
    handleDialogOpen ({ mode }) {},
    addRow () {},
    addRowWithNewTemplate () {}
  }
}
</script>
